package com.gmu.uav;

import android.graphics.*;
import android.util.Log;

public class DronePacket {

	private DroneCode m_DroneCode;
	private Bitmap m_DroneJpeg;
	private int m_DroneLatitude;
	private int m_DroneLongitude;
	private int m_DroneAltitude;
	
	private enum DroneCode
	{
		VIDEO_DATA, GPS_DATA, THRUST_DATA, SONAR_DATA
	}
	
	public DronePacket(byte[] inBuffer) 
	{	
		switch(inBuffer[0])
		{
			case 0x01:
				m_DroneCode = DroneCode.VIDEO_DATA;
				break;
			case 0x02:
				m_DroneCode = DroneCode.GPS_DATA;
				break;
			case 0x03:
				m_DroneCode = DroneCode.THRUST_DATA;
				break;
			case 0x04:
				m_DroneCode = DroneCode.SONAR_DATA;
				break;
			default:
				Log.i("GMU ECE798 UAV", "Invalid DronePacket DroneCode!");
				break;
		}
				
		switch(m_DroneCode)
		{
			case VIDEO_DATA:
				setDroneJPEG(inBuffer, 1);
				break;

			case GPS_DATA:
				setDroneGPS(inBuffer, 1);
				break;
			
			case SONAR_DATA:
				setDroneSonar(inBuffer, 1);
				break;
				
			default:
				break;
		}
	}
	
	private void setDroneJPEG(byte[] buffer, int bufferOffset)
	{
		m_DroneJpeg.recycle();
		m_DroneJpeg = BitmapFactory.decodeByteArray(buffer, bufferOffset, buffer.length );
	}
	
	private void setDroneGPS(byte[] buffer, int bufferOffset)
	{
		System.arraycopy(buffer, bufferOffset, m_DroneLatitude, 0, 4);
		System.arraycopy(buffer, bufferOffset + 4, m_DroneLongitude, 0, 4);
	}
		
	private void setDroneSonar(byte[] buffer, int bufferOffset)
	{
		System.arraycopy(buffer, bufferOffset, m_DroneAltitude, 0, 4);
	}
	
	public Bitmap getDroneJPEG()
	{
		if(m_DroneJpeg != null)
		{
			return m_DroneJpeg;
		}
		else
			return null;
	}
	
	public int getDroneLatitude()
	{
		if(m_DroneLatitude != 0)
		{
			return m_DroneLatitude;
		}
		else 
			return -1;
	}
	
	public int getDroneLongitude()
	{
		if(m_DroneLongitude != 0)
		{
			return m_DroneLongitude;
		}
		else 
			return -1;
	}
	
	public int getDroneAltitude()
	{
		if(m_DroneAltitude != 0)
		{
			return m_DroneAltitude;
		}
		else
			return -1;
	}
}
